package com.happybluefin.tapburstfree.application;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import net.cavas.show.MainLoadCavasManager;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.happybluefin.tapburstfree.R;
import com.happybluefin.tapburstfree.message.Define;
import com.happybluefin.tapburstfree.common.data.RomData;
import com.happybluefin.tapburstfree.common.scoreloop.Scoreloop;
import com.scoreloop.client.android.ui.ScoreloopManagerSingleton;
import com.umeng.analytics.MobclickAgent;

public class TapBurstFreeApplication extends Application {
    public void onCreate() {
        Log.d(TAG, "onCreate() start");

        mContext = this;
//        Client.init(this, secret, null);

        ScoreloopManagerSingleton.init(this, RomData.SCORELOOP_SECRET);

        // init language array
        _initLanguage();

        // load online config
        _loadOnlineConfig();

        Log.d(TAG, "onCreate() end");
    }

    public static void reportScore(String boardName, int score) {
        Log.d(TAG, "reportScore() start");

        if (mContext != null) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(Define.Action.ACTION);
            bundle.putInt("TYPE", Define.MessageID.REPORT_SCORE);
            bundle.putString("BOARDNAME", boardName);
            bundle.putInt("SCORE", score);
            intent.putExtras(bundle);
            mContext.sendBroadcast(intent);
        }
        else {
            Log.e(TAG, "reportScore(): mContext is NULL.");
        }

        Log.d(TAG, "reportScore() end");
    }

    public static void showLeaderBoard(String boardName) {
        Log.d(TAG, "showLeaderBoard() start");

        if (mContext != null) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(Define.Action.ACTION);
            bundle.putInt("TYPE", Define.MessageID.SHOW_LEADERBOARD);
            bundle.putString("BOARDNAME", boardName);
            intent.putExtras(bundle);
            mContext.sendBroadcast(intent);
        }
        else {
            Log.e(TAG, "showLeaderBoard(): mContext is NULL.");
        }

        Log.d(TAG, "showLeaderBoard() end");
    }

    public static void shareToSinaWeibo(String text) {
        Log.d(TAG, "shareToSinaWeibo() start");

        if (mContext != null) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(Define.Action.ACTION);
            bundle.putInt("TYPE", Define.MessageID.SINA_WEIBO_SHARE);
            bundle.putString("TEXT", text);
            intent.putExtras(bundle);
            mContext.sendBroadcast(intent);
        }
        else {
            Log.e(TAG, "shareToSinaWeibo(): mContext is NULL.");
        }

        Log.d(TAG, "shareToSinaWeibo() end");
    }

    public static void shareToTencentWeibo(String text) {
        Log.d(TAG, "shareToTencentWeibo() start");

        if (mContext != null) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(Define.Action.ACTION);
            bundle.putInt("TYPE", Define.MessageID.TENCENT_WEIBO_SHARE);
            bundle.putString("TEXT", text);
            intent.putExtras(bundle);
            mContext.sendBroadcast(intent);
        }
        else {
            Log.e(TAG, "shareToTencentWeibo(): mContext is NULL.");
        }

        Log.d(TAG, "shareToSinaWeibo() end");
    }

    public static void gotoUrl(String url) {
        Log.d(TAG, "gotoUrl() start");

        if (mContext != null) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(Define.Action.ACTION);
            bundle.putInt("TYPE", Define.MessageID.WEB_BROWSER);
            bundle.putString("URL", url);
            intent.putExtras(bundle);
            mContext.sendBroadcast(intent);
        }
        else {
            Log.e(TAG, "gotoUrl(): mContext is NULL.");
        }

        Log.d(TAG, "gotoUrl() end");
    }

    public static String getLocalizedString(String index) {
        Log.d(TAG, "getLocalizedString() start");

        String text = index;

        if ((mContext != null) && (index != null)) {
            Log.d(TAG, "getLocalizedString() index: " + index);
//          mContext.getString(resId);
            
            text = mLanguage.get(index);
            if (text == null) {
                Log.e(TAG, "getLocalizedString(): unknow index: " + index);
                text = index;
            }
        }

        Log.d(TAG, "getLocalizedString() end");

        return text;
    }

    public static boolean isChinese() {
        Log.d(TAG, "isChinese() start");

        boolean ret = false;
        
        Locale locale = Locale.getDefault();
        String language = locale.getLanguage();
        
        if (language.equals("zh")) {
            ret = true;
        }

        Log.d(TAG, "isChinese() end");

        return ret;
    }

    public static String getCountryCode() {
        Log.d(TAG, "getCountryCode() start");

        Locale locale = Locale.getDefault();
        String country = locale.getCountry();

        Log.d(TAG, "getCountryCode() end");

        return country;
    }

    public static String getLanguageCode() {
        Log.d(TAG, "getLanguageCode() start");

        Locale locale = Locale.getDefault();
        String language = locale.getLanguage();

        Log.d(TAG, "getLanguageCode() end");

        return language;
    }

    public static int umengGetParamValue(String name) {
        Log.d(TAG, "umengGetParamValue() start");

        int ret = 0;
        
        if (mContext != null) {
            String value = MobclickAgent.getConfigParams(mContext, name);
            try {
                mIsFBEnable = Integer.parseInt(value);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        Log.d(TAG, "umengGetParamValue() end");
        
        return ret;
    }
    
    public static void umengCustomEvent(String name, String value) {
        Log.d(TAG, "umengCustomEvent() start");

        if (mContext != null) {
            MobclickAgent.onEvent(mContext, name, value);
        }

        Log.d(TAG, "umengCustomEvent() end");
    }
    
    public static void openAdsMogoOffer() {
        Log.d(TAG, "openAdsMogoOffer() start");

        if (mContext != null) {
            Bundle bundle = new Bundle();
            Intent intent = new Intent(Define.Action.ACTION);
            bundle.putInt("TYPE", Define.MessageID.OPEN_ADS_MOGO_OFFER);
            intent.putExtras(bundle);
            mContext.sendBroadcast(intent);
        }
        else {
            Log.e(TAG, "openAdsMogoOffer(): mContext is NULL.");
        }

        Log.d(TAG, "openAdsMogoOffer() end");
    }

    private void _initLanguage() {
        Log.d(TAG, "_initLanguage() start");

        mLanguage.put("fontNormal", getString(R.string.font_normal));
        mLanguage.put("language", getString(R.string.language));
        mLanguage.put("Level Select", getString(R.string.level_select));
        mLanguage.put("Goal", getString(R.string.goal));
        mLanguage.put("Time", getString(R.string.time));
        mLanguage.put("Start", getString(R.string.start));
        mLanguage.put("Result", getString(R.string.result));
        mLanguage.put("Mission %d", getString(R.string.mission_x));
        mLanguage.put("Close", getString(R.string.close));
        mLanguage.put("Mission Failed!", getString(R.string.mission_failed));
        mLanguage.put("Give Up", getString(R.string.give_up));
        mLanguage.put("Leaderboard", getString(R.string.leaderboard));
        mLanguage.put("More Game", getString(R.string.more_game));
        mLanguage.put("Retry", getString(R.string.retry));
        mLanguage.put("High score!", getString(R.string.high_score1));
        mLanguage.put("High score:", getString(R.string.high_score2));
        mLanguage.put("Mission Complete!", getString(R.string.mission_complete));
        mLanguage.put("All Clear!!!", getString(R.string.all_clear));
        mLanguage.put("Play Single", getString(R.string.play_single));
        mLanguage.put("Play Double", getString(R.string.play_double));
        mLanguage.put("Chellange", getString(R.string.chellange));
        mLanguage.put("Next Goal:", getString(R.string.next_goal));
        mLanguage.put("<< Back", getString(R.string.back));
        mLanguage.put("Again", getString(R.string.again));
        mLanguage.put("Next", getString(R.string.next));
        mLanguage.put("TOUCH TO START", getString(R.string.touch_to_start));
        mLanguage.put("High:", getString(R.string.high));
        mLanguage.put("FINISH", getString(R.string.finish));
        mLanguage.put("ShareMsgNormal", getString(R.string.share_msg_normal));
        mLanguage.put("ShareMsgChellange", getString(R.string.share_msg_chellange));
        mLanguage.put("Come to challange highest score!", getString(R.string.come_to_challange_highest_score));
        mLanguage.put("Your friend get a new higher score. Beat It!", getString(R.string.your_friend_get_a_new_higher_score_beat_it));

        Log.d(TAG, "_initLanguage() end");
    }

    private void _loadOnlineConfig() {
        Log.d(TAG, "_loadOnlineConfig() start");

        String value = null;
        value = MobclickAgent.getConfigParams(this, "isFBEnable");
        if (value != null) {
            try {
                mIsFBEnable = Integer.parseInt(value);
            }
            catch (Exception e) {
                e.printStackTrace();
                mIsFBEnable = 0;
            }
        }

        value = MobclickAgent.getConfigParams(this, "isAdBannerEnable");
        if (value != null) {
            try {
                mIsAdBannerEnable = Integer.parseInt(value);
            }
            catch (Exception e) {
                e.printStackTrace();
                mIsAdBannerEnable = 1;
            }
        }

        value = MobclickAgent.getConfigParams(this, "isMoreEnable");
        if (value != null) {
            try {
                mIsMoreEnable = Integer.parseInt(value);
            }
            catch (Exception e) {
                e.printStackTrace();
                mIsMoreEnable = 0;
            }
        }

        value = MobclickAgent.getConfigParams(this, "isAdPushEnable");
        if (value != null) {
            try {
                mIsAdPushEnable = Integer.parseInt(value);
            }
            catch (Exception e) {
                e.printStackTrace();
                mIsAdPushEnable = 0;
            }
        }

        Log.d(TAG, "_loadOnlineConfig() end");
    }

    protected static Context mContext = null;

    protected static Scoreloop mScoreloop = null;

//    protected static final String SCORELOOP_SECRET = "Ll9Mu+lM4CzwxZa+yezxBHDBMS4lZpUBY/OA+7pW3tQys+dnwuaNJg==";

    protected static final String TAG = "TapBurstFreeApplication";

    protected static Map<String, String> mLanguage = new HashMap<String, String>();

    public static int mIsFBEnable = 0;

    public static int mIsAdBannerEnable = 1;

    public static int mIsMoreEnable = 0;

    public static int mIsAdPushEnable = 0;
}
